public class IntBuffer {
    int[] buffer = new int[8];

    /**
     * the index of the current position in the buffer.
     * This value ranges from -1 (empty buffer) to buffer length - 1 (full)
     */
    int index = -1;

    protected Object theLock = new Object();

    public void add(int value) throws Exception {
        synchronized (theLock) {
            if (index >= buffer.length - 1) {
                theLock.wait();
            }
            index++;
            buffer[index] = value;
            theLock.notify();
        }
    }

    public int remove() throws Exception {
        synchronized (theLock) {
            if (index < 0) {
                theLock.wait();
            }
            int value = buffer[index];
            index--;
            theLock.notify();
            return value;
        }
    }
}
